package com.example.travel.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileSave {


    public static String serverPrefix ;

    public static String saveMultipartFile(MultipartFile file,String uploadPath) throws IOException {
        System.out.println(uploadPath);
        File dir = new File(uploadPath+"/image");
        if (!dir.exists()){
            dir.mkdirs();
        }

        String prefix = UUID.randomUUID().toString().replace("-","");
        int index = file.getOriginalFilename().lastIndexOf(".");

        String suffix = file.getOriginalFilename().substring(index);
        String newName = prefix+suffix;
        file.transferTo(new File(dir,newName));

        String url = serverPrefix + newName;
        System.out.println(url);
        return url;
    }
}
